//
//  CameraViewController.m
//  Camera
//
//  Created by gml on 2018/9/29.
//  Copyright © 2018年 gml. All rights reserved.
//

#import "CameraViewController.h"
#import "AipCameraPreviewView.h"
#import "AipCameraController.h"

@interface CameraViewController ()

@property (nonatomic,strong) AipCameraPreviewView *previewView;
@property (strong, nonatomic) AipCameraController *cameraController;

@end

@implementation CameraViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.cameraController = [[AipCameraController alloc] initWithCameraPosition:AVCaptureDevicePositionBack];
    
//    self.navigationController.navigationBarHidden = YES;
    self.previewView.translatesAutoresizingMaskIntoConstraints = NO;
    self.previewView.session = self.cameraController.session;
    
    [self.view addSubview:self.previewView];
    
    [self.cameraController startRunningCamera];

//    UIView *maskView = [[UIView alloc] initWithFrame:self.view.frame];
//    maskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
//    [self.view addSubview:maskView];
//    
//    UIImage *maskImage = [UIImage imageNamed:@"右后黑"];
//    CALayer *layer = [[CALayer alloc] init];
//    layer.frame = CGRectMake(0, 0, maskImage.size.width, maskImage.size.height);
//    layer.contents = (__bridge id _Nullable)(maskImage.CGImage);
//    layer.position = self.view.center;
//    maskView.layer.mask = layer;
    
//
//    CGRect whiteFrame = CGRectMake(10, 10, maskView.frame.size.width - 20, (maskView.frame.size.height - 20)*3/4);
//    UIView *centerView = [[UIView alloc] initWithFrame:whiteFrame];
//    centerView.layer.borderColor = [UIColor whiteColor].CGColor;
//    centerView.layer.borderWidth = 1.0;
//    centerView.center = maskView.center;
//    [self.view addSubview:centerView];
//
//    //贝塞尔曲线 画一个带有圆角的矩形
//    UIBezierPath *bpath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, maskView.frame.size.width, maskView.frame.size.height) cornerRadius:0];
////    贝塞尔曲线 画一个圆形
////    [bpath appendPath:[UIBezierPath bezierPathWithArcCenter:maskView.center radius:100 startAngle:0 endAngle:2*M_PI clockwise:NO]];
//    [bpath appendPath:[[UIBezierPath bezierPathWithRoundedRect:centerView.frame cornerRadius:0] bezierPathByReversingPath]];
//    //创建一个CAShapeLayer 图层
//    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
//    shapeLayer.path = bpath.CGPath;
//    shapeLayer.frame = CGRectMake(0, 0, maskImage.size.width, maskImage.size.height);
////    shapeLayer.contents = (__bridge id _Nullable)(maskImage.CGImage);
//    shapeLayer.position = self.view.center;
//    maskView.layer.mask = shapeLayer;
   
    
   
  
}
- (void)setImageName:(NSString *)imageName
{
    _imageName = imageName;
    [self maskView1WithImagName:imageName];
}
//MARK: 中间矩形镂空
-(void)maskView1WithImagName:(NSString *)imamgeName
{
    // 添加示意图
    UIImage *tipImage = [UIImage imageNamed:imamgeName];
    //创建一个View
    UIView *maskView = [[UIView alloc] initWithFrame:self.view.frame];
    maskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [self.view addSubview:maskView];
    
    
    CGRect whiteFrame = CGRectMake(([UIScreen mainScreen].bounds.size.width-tipImage.size.width)*0.5, ([UIScreen mainScreen].bounds.size.height-tipImage.size.height)*0.5, tipImage.size.width, tipImage.size.height);
    UIView *centerView = [[UIView alloc] initWithFrame:whiteFrame];
    centerView.layer.borderColor = [UIColor whiteColor].CGColor;
    centerView.layer.borderWidth = 1.0;
    //    centerView.layer.cornerRadius = ;
    centerView.center = maskView.center;
    [self.view addSubview:centerView];
    
   
    UIImageView *tipImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, centerView.frame.size.width, centerView.frame.size.height)];
    tipImageView.contentMode = UIViewContentModeScaleAspectFit;
    tipImageView.image = tipImage;
    [centerView addSubview:tipImageView];
    
    //贝塞尔曲线 画一个带有圆角的矩形
    UIBezierPath *bpath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, maskView.frame.size.width, maskView.frame.size.height) cornerRadius:0];
    //贝塞尔曲线 画一个圆形
    //    [bpath appendPath:[UIBezierPath bezierPathWithArcCenter:maskView.center radius:100 startAngle:0 endAngle:2*M_PI clockwise:NO]];
    [bpath appendPath:[[UIBezierPath bezierPathWithRoundedRect:centerView.frame cornerRadius:0] bezierPathByReversingPath]];
    //创建一个CAShapeLayer 图层
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = bpath.CGPath;
    shapeLayer.fillColor = [UIColor whiteColor].CGColor;
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    shapeLayer.lineWidth = 1.0f;
    //添加图层蒙板
    maskView.layer.mask = shapeLayer;
}

- (AipCameraPreviewView *)previewView
{
    if (_previewView == nil) {
        _previewView = [[AipCameraPreviewView alloc] initWithFrame:self.view.frame];
//        _previewView.backgroundColor = [UIColor whiteColor];
    }
    return _previewView;
}


@end
